package cn.elwy.rcp.ui.explorer.core.util;

import java.io.File;
import java.util.List;

import org.apache.maven.shared.invoker.DefaultInvocationRequest;
import org.apache.maven.shared.invoker.DefaultInvoker;
import org.apache.maven.shared.invoker.InvocationRequest;
import org.apache.maven.shared.invoker.InvocationResult;
import org.apache.maven.shared.invoker.Invoker;

import cn.elwy.common.exception.RunException;

/**
 * Maven命令调用工具类
 * @author huangsq
 * @version 1.0, 2014-09-19
 * @since 1.0, 2014-09-19
 */
public class MavenUtil {

	public static InvocationResult execute(File file, File mavenHome, List<String> commandList) {
		return execute(file, getMavenInvoker(mavenHome), commandList);
	}

	public static InvocationResult execute(File file, Invoker invoker, List<String> commandList) {
		InvocationRequest request = new DefaultInvocationRequest();
		request.setPomFile(file);
		request.setGoals(commandList);

		try {
			return invoker.execute(request);
		} catch (Exception e) {
			throw new RunException(e.getMessage(), e);
		}
	}

	public static Invoker getMavenInvoker(File file) {
		Invoker invoker = new DefaultInvoker();
		if (file != null && file.exists()) {
			invoker.setMavenHome(file);
		}
		return invoker;
	}

}